layui.use(['form', 'laypage', 'admin', 'ax'], function () {
    let $ = layui.jquery;
    let $ax = layui.ax;
    let ajax = new $ax(Feng.ctxPath + "/stock/stockStrategy/detail?id=" + Feng.getUrlParam("id"));
    let result = ajax.start();
    let stockCode = result.data.code

    openSocket(stockCode)
    function openSocket(stockCode) {
        let socket;
        if (typeof (WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        } else {
            console.log("您的浏览器支持WebSocket");
            //实现化WebSocket对象，指定要连接的服务器地址与端口  建立连接
            // var socketUrl="ws://127.0.0.1:22599/webSocket/"+userId;
            let socketUrl = "ws://www.lemego.xyz:8086/webSocket/" + stockCode;
            // let socketUrl = "ws://localhost:8086/webSocket/" + stockCode;
            console.log(socketUrl);
            if (socket != null) {
                socket.close();
                socket = null;
            }
            socket = new WebSocket(socketUrl);
            //打开事件
            socket.onopen = function () {
                console.log("websocket已打开");
                //socket.send("这是来自客户端的消息" + location.href + new Date());
            };
            //获得消息事件
            socket.onmessage = function (msg) {
                var serverMsg = "收到服务端信息：" + msg.data;
                var stockInfo = JSON.parse(msg.data);
                document.getElementById('stockInfo').innerHTML = stockInfo.stockName +
                    "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.round(stockInfo.priceChange * 100) / 100 + "%" + "&nbsp;&nbsp;&nbsp;&nbsp;" + stockInfo.close;
                document.getElementById('buyFirst').innerHTML = stockInfo.buyFirst + "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.ceil(stockInfo.buyFirstNumber / 100);
                document.getElementById('buySecond').innerHTML = stockInfo.buySecond + "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.ceil(stockInfo.buySecondNumber / 100);
                document.getElementById('buyThird').innerHTML = stockInfo.buyThird + "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.ceil(stockInfo.buyThirdNumber / 100);
                document.getElementById('buyFourth').innerHTML = stockInfo.buyFourth + "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.ceil(stockInfo.buyFourthNumber / 100);
                document.getElementById('buyFive').innerHTML = stockInfo.buyFive + "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.ceil(stockInfo.buyFiveNumber / 100);
                document.getElementById('sellFirst').innerHTML = stockInfo.sellFirst + "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.ceil(stockInfo.sellFirstNumber / 100);
                ;
                document.getElementById('sellSecond').innerHTML = stockInfo.sellSecond + "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.ceil(stockInfo.sellSecondNumber / 100);
                ;
                document.getElementById('sellThird').innerHTML = stockInfo.sellThird + "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.ceil(stockInfo.sellThirdNumber / 100);
                ;
                document.getElementById('sellFourth').innerHTML = stockInfo.sellFourth + "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.ceil(stockInfo.sellFourthNumber / 100);
                ;
                document.getElementById('sellFive').innerHTML = stockInfo.sellFive + "&nbsp;&nbsp;&nbsp;&nbsp;" + Math.ceil(stockInfo.sellFiveNumber / 100);
                ;
                document.getElementById('stockImage').src = "http://image.sinajs.cn/newchart/min/n/" + stockInfo.stockCode + ".gif";
                // document.getElementById('original').value=msg.data;

                //发现消息进入    开始处理前端触发逻辑
            };
            //关闭事件
            socket.onclose = function () {
                console.log("websocket已关闭");
            };
            //发生了错误事件
            socket.onerror = function () {
                console.log("websocket发生了错误");
            }
        }
    }

    function sendMessage() {
        if (typeof (WebSocket) == "undefined") {
            console.log("您的浏览器不支持WebSocket");
        } else {
            // console.log("您的浏览器支持WebSocket");
            var toUserId = document.getElementById('toUserId').value;
            var contentText = document.getElementById('contentText').value;
            var msg = '{"toUserId":"' + toUserId + '","contentText":"' + contentText + '"}';
            console.log(msg);
            socket.send(msg);
        }
    }

});


